package top.leinx.fxnotes.imageview;

import javafx.fxml.FXML;
import javafx.geometry.Rectangle2D;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author : xiaolei
 * @DateTime: 2021/3/15 11:01 下午
 */
public class ImageViewController {
    @FXML
    private Button buttonLoad;
    @FXML
    private Button buttonTranslate;
    @FXML
    private Button buttonRotate;
    @FXML
    private Button buttonScale;
    @FXML
    private Button buttonShear;
    @FXML
    private ImageView imageView;
    @FXML
    private TextField tfFilename;
    @FXML
    private TextField tfWidth;
    @FXML
    private TextField tfHeight;
    @FXML
    private CheckBox cbKeepRatio;
    @FXML
    private Label labelWidth;
    @FXML
    private Label labelHeight;

    @FXML
    private TextField tfTranslateX;
    @FXML
    private TextField tfTranslateY;
    @FXML
    private TextField tfRotateDegree;
    @FXML
    private CheckBox cbContinuousRotation;
    @FXML
    private TextField tfScaleRatio;
    @FXML
    private CheckBox cbContinuousScale;


    private Alert alert = new Alert(Alert.AlertType.ERROR, "出错信息"); // 提示对话框

    @FXML
    public void loadImage() {
        File imageFile = new File(tfFilename.getText());  // 从文本输入框获取文件名并创建文件对象

        // 文件不存在的处理
        if (!imageFile.exists()) {
            alert.setContentText("指定的图像文件不存在！！！");
            alert.showAndWait();
            return;
        }

        // 检测指定的宽度和高度格式是否正确，如果正确进行类型转换
        double width, height;
        if (!(tfWidth.getText().matches("[0-9]+\\.[0-9]+") && tfHeight.getText().matches("[0-9]+\\.[0-9]+"))) {
            alert.setContentText("指定的宽度或高度的数据格式不对，应该是正实数！！！");
            alert.showAndWait();
            return;
        } else {
            width = Double.valueOf(tfWidth.getText());
            height = Double.valueOf(tfHeight.getText());
        }

        Image image = null;

        // 使用文件 URL 加载图像
        /*
        image = new Image(
                imageFile.toURI().toString(),
                width,
                height,
                cbKeepRatio.isSelected(),
                true);

         */

        // 使用流方式加载图像
        try (InputStream is = new FileInputStream(imageFile)) {
            image = new Image(
                    is,
                    width,
                    height,
                    cbKeepRatio.isSelected(),
                    true);
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            alert.setContentText("加载图像过程出错！");
            alert.showAndWait();
            return;
        }

        // 显示加载的图像的实际宽度和高度
        labelWidth.setText(String.valueOf(image.getWidth()));
        labelHeight.setText(String.valueOf(image.getHeight()));

        // 把加载的图像显示到 imageView
        imageView.setImage(image);
    }

    @FXML
    public void translateImage() {
        double x, y;
        if (!(tfTranslateX.getText().matches("[\\-]{0,1}[0-9]+\\.[0-9]+") && tfTranslateY.getText().matches("[\\-]{0,1}[0-9]+\\.[0-9]+"))) {
            alert.setContentText("平移指定的 x 或 y 的数据格式不对，应该是实数！！！");
            alert.showAndWait();
            return;
        } else {
            x = Double.valueOf(tfTranslateX.getText());
            y = Double.valueOf(tfTranslateY.getText());
        }

        imageView.setTranslateX(x);
        imageView.setTranslateY(y);
    }

    @FXML
    public void rotateImage() {
        double degree;
        if (!(tfRotateDegree.getText().matches("[\\-]{0,1}[0-9]+\\.[0-9]+"))) {
            alert.setContentText("指定的旋转度数格式不对，应该是实数！！！");
            alert.showAndWait();
            return;
        } else {
            degree = Double.valueOf(tfRotateDegree.getText());
        }
        // 创建并设置旋转视口
        Rectangle2D viewport = new Rectangle2D(0, 0, imageView.getImage().getWidth(), imageView.getImage().getHeight());
        imageView.setViewport(viewport);

        // 旋转变换
        if (cbContinuousRotation.isSelected()) {
            imageView.setRotate(imageView.getRotate() + degree); // 在上次旋转的基础上继续旋转
        } else {
            imageView.setRotate(degree);                         // 在源图像上转换
        }
        System.out.println("ImageView: " + imageView.getFitWidth() + ", " + imageView.getFitHeight());
        System.out.println("ImageView.getImage: " + imageView.getImage().getWidth() + ", " + imageView.getImage().getHeight());
    }

    @FXML
    public void scaleImage() {
        double scaleRatio;
        if (!(tfScaleRatio.getText().matches("[0-9]+\\.[0-9]+"))) {
            alert.setContentText("指定的绽放比例的数据格式不对，应该是正实数！！！");
            alert.showAndWait();
            return;
        } else {
            scaleRatio = Double.valueOf(tfScaleRatio.getText()) / 100.0;
        }

        // 缩放变换，x 和 y 方向使用相同比例，也可以不相同
        if (cbContinuousScale.isSelected()) {
            imageView.setScaleX(imageView.getScaleX() * scaleRatio);
            imageView.setScaleY(imageView.getScaleY() * scaleRatio);
        } else {
            imageView.setScaleX(scaleRatio);
            imageView.setScaleY(scaleRatio);
        }

    }

}
